<?
	session_start();
	include_once 'functionsDocs.php';
	include_once 'functions_db.php';
	include 'headermenu.php';
//foarte tare??

    $rowid=$_GET[rowid];
	$idd=$_GET[iddoc]; 
	$idp=$_GET[idp];
	$del=$_GET[del];
	//Conectare la DB:
	$dbh=dbConn();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<!-- <link rel="stylesheet" type="text/css" href="default.css"> -->
<!--
-->
<link rel="stylesheet" href="yuinou.css" type="text/css">
<head>
	<title>Methodology Portal</title>
</head>

<body>
<?php   
     //modificare: prin if-ul de mai jos se verifica daca s-a realizat logarea; deasemenea acesta trebuie sa apara in fiecare pagina
	if(ISSET($_SESSION["userId"]) == FALSE)
    {
         echo "<script type =\"text/javascript\">window.location.href=\"methodology.php\";</script>";
		 
		 exit();
	}  
?>
<div id="doc" class="yui-t7">
  
   <div id="bd" role="main">
 
 
 <div class="yui-gc">
    <div class="yui-u first">
	<!-- YOUR DATA GOES HERE -->
<!--	<div align="left"> <br><a href='project_stages.php?rowid=<? echo $idp; ?>' onclick="return backhref(this)">Back</a> </div> -->
	<div align="right" >
	<table  bgcolor="#8ED9F9">
    <tr>
    <td bgcolor="#8ED9F9" style="vertical-align:middle"><form><input type="button" value="Back" name="back" action='project_stages.php?rowid=<? echo $idp; ?>' onclick="return backhref(this)"></form></td>
	<td bgcolor="#8ED9F9" style="vertical-align:middle">
	 <?
	echo "<form name=\"testQR\" method=\"post\" action='testpdf.php?iddoc=".$idd."&amp;idp=".$idp."'>";
	echo "<input type=\"submit\" name=\"pdf\" value=\"Generate PDF\" align=\"right\" >";
    echo "</form>";
	?>
	</td>
   <td bgcolor="#8ED9F9" style="vertical-align:middle"> <form action="" name="testQR" method="post" >
 <input type="submit" name="add" value="Add Title/Subtitle" align="right"> </form></td>
    </tr>
	</table>
	    </div>
		
    <div class="yui-u">
	<!-- YOUR DATA GOES HERE -->
	    </div>
 </div>
  </div>

 <div class="yui-gc">
    <div class="yui-u first" >
 <!-- PRIMUL ECRAN!!! -->
 
	<script type="text/javascript"> 
		function backhref(a) {
		if (document.referer == a.href) 
		{
		history.back();
		return false
		}
		}
	</script>


	<?
	
	//@@@@ BACK BUTTON @@@@//
	
	if(isset($_POST['back']))
	{
		refreshPage("project_stages.php?rowid=".$idp."");
	}
	
	//@@@@ THE EVIL DELETE BUTTON! @@@@//
	if (isset($_GET['del']))
	{
	$resultd = returnQuery("select * from document_body where id=".$rowid." and id_doc= '".$idd."';", $dbh);
	$rowd= mysql_fetch_assoc($resultd);
	if ($rowd['type']=='Subtitle')
		{
		$resultpui = returnQuery("delete from document_body where parent=".$rowid." and id_doc= '".$idd."';", $dbh);
		$resultdel = returnQuery("delete from document_body where id=".$rowid." and id_doc= '".$idd."';", $dbh);
		}
	else
		{
		$resultpui = returnQuery("select * from document_body where parent=".$rowid." and id_doc= '".$idd."';", $dbh);
		while ($rowpui= mysql_fetch_assoc($resultpui))
		{
			$resultcon = returnQuery("delete from document_body where parent=".$rowpui['id']." and id_doc= '".$idd."';", $dbh);
			$resultp = returnQuery("delete from document_body where id=".$rowpui['id']." and id_doc= '".$idd."';", $dbh);
		}
		$resultdel = returnQuery("delete from document_body where id=".$rowid." and id_doc= '".$idd."';", $dbh);
		
		}
	refreshPage("documents.php?idp=".$idp."&amp;iddoc=".$idd."");

	
	}
	
	
	
	
	//####MODIFICA SECTIUNE:
	if(isset($_POST['qr_update_c']))
	{
	$resulte = returnQuery("update document_body set content='".$_POST["content"]."' where id=".$rowid." and id_doc= '".$idd."';", $dbh);
	unset($_GET['rowid']);
	refreshPage("documents.php?idp=".$idp."&amp;iddoc=".$idd."");

	}
	

	//####ADAUGA SECTIUNE:
	if(isset($_POST['qr_add']))
	{
		//#### Adauga Titlu:
		//#### Caut orderT al urmatorului titlu, daca exista:
		
		if ($_POST["sel_type"]=='Title') 
		{
			if ($_POST["sel_place"]==0) 
			{
				$queryNextTitle="SELECT orderT FROM `document_body` where id_doc='".$idd."' ORDER BY orderT limit 0,1";
			} 
			else 
			{
				$queryNextTitle="SELECT orderT  FROM `document_body` WHERE orderT > (select orderT from document_body where id = ".$_POST['sel_place']." and id_doc='".$idd."') and id_doc='".$idd."' ORDER BY orderT limit 0,1";
				$queryIFlast="SELECT count(orderT) nr from document_body where id = ".$_POST['sel_place']." and id_doc='".$idd."'";
			}
			$resultTitle=mysql_query($queryNextTitle);
			$rowTitle = mysql_fetch_assoc($resultTitle);
			
			$resultIFlast=mysql_query($queryIFlast);
			if($resultIFlast)
			{			
			$rowIFlast= mysql_fetch_assoc($resultIFlast);
			}
			else $rowIFlast=0;
			
		
			//#### Caut orderT pentru id-ul pasat din selectie:
			$queryNextTitleSel="SELECT orderT FROM `document_body` WHERE id = ".$_POST['sel_place']." and id_doc='".$idd."';";
			$resultTitleSel=mysql_query($queryNextTitleSel);
			
				
				
				
				//#### Inserez in DB dupa caz:
				if (mysql_num_rows($resultTitle) == 1)
				{	$rowTitleSel = mysql_fetch_assoc($resultTitleSel);
					$newOrderT= (int) (($rowTitle['orderT']+$rowTitleSel['orderT'])/2);
					$queryInsTitle=	"insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'WRITE THE NEW TITLE HERE!!!', 0,".$newOrderT.", 0, 0, 'Title');";
					mysql_query($queryInsTitle);
					
				} 
				else 
				{
					if ((mysql_num_rows($resultTitle) == 0)&&($rowIFlast['nr']==0))
					{
						$newOrderT = 10000;
						$queryInsTitle=	"insert into document_body (id, id_project,  id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'WRITE THE NEW TITLE HERE!!!', 0,".$newOrderT.", 0, 0, 'Title');";
						mysql_query($queryInsTitle);
					}
					else
					{
					$rowTitleSel = mysql_fetch_assoc($resultTitleSel);
						if ($_POST["sel_place"]==0) {
							$newOrderT= (int) ($rowTitle['orderT']/2);
							$queryInsTitle=	"insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'WRITE THE NEW TITLE HERE!!!', 0,".$newOrderT.", 0, 0, 'Title');";
							mysql_query($queryInsTitle);
						} 
						else 
						{
						echo "row title sel ".$rowTitleSel['orderT'];
							$newOrderT=$rowTitleSel['orderT']+10000;
							$queryInsTitle=	"insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'WRITE THE NEW TITLE HERE!!!', 0,".$newOrderT.", 0, 0, 'Title');";
							mysql_query($queryInsTitle);
						}
					}
				}
			
				//echo ', newOrderT: -->'.$newOrderT;
				if (isset($_POST['content'])==true)
				{
					$queryContentParent=	"select max(id) as maxim from document_body where id_doc=".$idd.";";
					$rez=mysql_query($queryContentParent);
					$rowCP=mysql_fetch_assoc($rez);
					
					$queryInsContent=	"insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'TITLE CONTENT!!!', ".$rowCP['maxim'].",".$newOrderT.",0, 10000, 'Content');";
					mysql_query($queryInsContent);
					
				}
			
		
		} 
		else 
		{

			//#### Adauga Subtitlu:
			//echo 'tip: -->'.$_POST['sel_type'];
			//echo ', ID T selectat: -->'.$_POST['sel_place'];
			//#### Caut orderT al urmatorului titlu, daca exista:
			if ($_POST["sel_place"]==0) 
			{
				$queryNextTitle="SELECT orderT FROM `document_body` where id_doc='".$idd."' ORDER BY orderT limit 0,1";
				
			} 
			else 
			{ 
				$queryTorS="select * from document_body where id=".$_POST['sel_place']." and id_doc='".$idd."';";
				$resultTorS=mysql_query($queryTorS);
				$rowTorS=mysql_fetch_assoc($resultTorS);
				//echo "sel place type ".$rowTorS["type"];
				
				$queryTitle="SELECT orderT, orderS FROM `document_body` where id=".$_POST['sel_place']." and id_doc='".$idd."';";
				// WHERE orderT > (select orderT from document_body where id = ".$_POST['sel_place'].") ORDER BY orderT limit 0,1";
				$resultTitle=mysql_query($queryTitle);
				$rowTitle=mysql_fetch_assoc($resultTitle);
				//echo 	"asta ".$rowTitle["orderT"];
				
				
				$querySubtitle="SELECT orderS FROM `document_body` where orderT='".$rowTitle["orderT"]."' and id_doc='".$idd."' and orderS > (select orderS from document_body where id = '".$_POST['sel_place']."' and id_doc='".$idd."') ORDER BY orderS limit 0,1;";
				$queryIFlastb="SELECT count(orderS) nr from document_body where parent = ".$_POST['sel_place']." and orderS <>0 and id_doc='".$idd."'";
				
				
				$resultSubtitle=mysql_query($querySubtitle);
				$resultIFlastb=mysql_query($queryIFlastb);
					if($resultIFlastb)
					{			
					$rowIFlastb= mysql_fetch_assoc($resultIFlastb);
					}
					else $rowIFlastb=0;
				echo "row if last: ".$rowIFlastb['nr'];
						
					
				if ((mysql_num_rows($resultSubtitle)==0)&&($rowIFlastb['nr']==0))	
					{
						$newOrderS= 10000;	
						echo "unu";
					
					}
				else if($rowTitle["orderS"]==0)
					{
						$rowSubtitle=mysql_fetch_assoc($resultSubtitle);
						if ($rowIFlastb['nr']==0)
						{
						$newOrderS=10000;
							echo "doi";
						}
						else { $newOrderS= (int) (($rowSubtitle['orderS'])/2);	}
						//echo "??".$rowIFlastb['nr'];	
					}
				else if($rowIFlastb['nr']==0)
					{
						$rowSubtitle=mysql_fetch_assoc($resultSubtitle);
						$newOrderS= (int) (($rowSubtitle['orderS']+$rowTitle["orderS"])/2);	
						echo "??3";	
						
					}
					else
					{
					
					$rowSubtitle=mysql_fetch_assoc($resultSubtitle);
						$newOrderS= (int) ((10000+$rowTitle["orderS"]));	//$rowSubtitle['orderS']
						echo "??2";	
						//echo $newOrderS;
					}
					
					if ($rowTorS['type']=='Title')
					
					{ $queryInsSubtitle=	"insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'WRITE THE NEW SubTITLE HERE!!!', ".$_POST['sel_place'].",".$rowTitle["orderT"].",".$newOrderS.", 0, 'Subtitle');";
					}
					else 
					{
					
					$queryInsSubtitle=	"insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'WRITE THE NEW SubTITLE HERE!!!', ".$rowTorS['parent'].",".$rowTitle["orderT"].",".$newOrderS.", 0, 'Subtitle');";
					}
					mysql_query($queryInsSubtitle);
					
				if (document.add_title_form.content.Checked==true)
					{
					$queryContentParent="select max(id) as maxim from document_body where id_doc=".$idd.";";
					$rez=mysql_query($queryContentParent);
					$rowCP=mysql_fetch_assoc($rez);
					
					$queryInsContent= "insert into document_body (id, id_project, id_doc, content, parent, orderT, orderS, orderC, type) values (null, ".$idp.", ".$idd.", 'CONTENT!!!', ".$rowCP['maxim']." ,".$rowTitle["orderT"].",".$newOrderS.", 10000, 'Content');";
					$resultl=mysql_query($queryInsContent);
					//echo ("merge la subtitlu");
					if (!$resultl)
					{
					die(mysql_error());
					}
					}
					
				
			}
	
		}
		if (document.add_title_form.content.Checked==true)
					{
				//	echo "afara";
					}
	}
	
	//###############################################################################################
	//##### AICI TREBUIE PUS SQL PENTRU LUAREA SIGLELOR si data DIN DB SI DE PUS IN HEADER!!!!!! ####
	//###############################################################################################
	
	//$pic_supplier = 'companies_pics/ec_logo.gif';
	//$pic_client = 'companies_pics/Vista-Printer.jpg';

//@@@@ SELECTAREA POZEI DIN DB @@@@//	
	
	
	$rescomp=returnQuery("Select c.id, c.picture, pc.id_company_type from companies c, project_companies pc where pc.id_project=".$idp." and c.id=pc.id_company;",$dbh);
	//static $j=0; 
	$j=0;
	while (($rowcomp=mysql_fetch_assoc($rescomp))&&($j<2))
		{
			if ($rowcomp['id_company_type']==0)
				{
				$pic_supplier=$rowcomp['picture'];
				$k=1;
				}
			else
			{
				$pic_client=$rowcomp['picture'];
				$i=1;
				}
				$j=k+i;
		}
			
	//echo "<img src='".$row['picture']."' width='50' height='50' /> </td></tr>";


//@@@@ SELECTAREA NUMELUI DE PROIECT DIN DB @@@@//		
	$resultd=mysql_query("select * from documents where id=".$idd.";");
	$rowd=mysql_fetch_assoc($resultd);
	
	$resultprj=mysql_query("select * from projects where id=".$idp.";");
	$rowprj=mysql_fetch_assoc($resultprj);
	
	//$today = date("d.m.Y");

	$ts_date = strtotime($rowd['date']);
	$data=date('d-m-Y',$ts_date);

	
//aici o sa inlocuim cuvantul edit cu <img src='images/delete.png' width='20' height='18' /> sau alta poza ;)
	echo '<br>';
	echo '<table width=100%><form action="" name="" method="post">';
	echo "<tr><td colspan='3'  bgcolor=\"#8ED9F9\">Header</td></tr>";
	echo "<tr><td rowspan='3' width=30><img src='".$pic_client."' width='50' height='50' /><br>Client Detailed Information</td><td align='center'>".$rowprj['name']."</td><td rowspan='3' width=30><img src='".$pic_supplier."' width='50' height='50' /><br>Supplier Detailed Information</td></tr>";
	echo "<tr><td align='center'>".$rowd['name']."</td></tr>";
	//echo "<tr><td>edit</td><td>Client Detailed Information</td><td align='end'>data</td><td>Supplier Detailed Information</td></tr>";
	echo "<tr><td align='end'>".$data."</td></tr></form>";
	echo '</table>';
	echo "<br>";

	
	//Afiseaza body-ul documentului:
	$result = returnQuery("select *, concat(orderT,'.',orderS,'.',orderC) orderby from document_body where id_doc=".$idd." order by orderT,orderS, orderby;", $dbh);
	echo '<table width=100%><form action="" name="testQR" method="post">';
	echo "<tr><td width='45'  bgcolor=\"#8ED9F9\">Meniu</td><td colspan='3'  bgcolor=\"#8ED9F9\">Document</td></tr>";
	//echo 
	"<tr>
		<td rowspan='3'> edit
		</td>
		<td><td>header</td><td></td></td>
		
	</tr>
	<tr>
		<td><td>header</td><td></td></td>
	</tr>
	<tr>
		<td><td>header</td><td></td></td>
	</tr>";


	
	while ($row = mysql_fetch_assoc($result)) {
		if ($row['type']=='Content')
		{
				echo "<tr><td align=right>
							
							<a href='documents.php?rowid=".$row["id"]."&amp;iddoc=".$idd."&amp;idp=".$idp." '>
								<img src='images/600px-Under_construction_icon-blue.svg.png' width='20' height='18' /> 
							</a>
							</td><p>";
			
		}
		else
		{
				echo "<tr><td>
							<a href='documents.php?rowid=".$row["id"]."&amp;iddoc=".$idd."&amp;idp=".$idp."&amp;del=1' > 
							<img src='images/delete.png' width='20' height='18' /> 
							</a> 
							<a href='documents.php?rowid=".$row["id"]."&amp;iddoc=".$idd."&amp;idp=".$idp." '>
								<img src='images/600px-Under_construction_icon-blue.svg.png' width='20' height='18' /> 
							</a>
							</td><p>";
		}
		if ($row["type"]=="Content") {
			if ($_GET["rowid"] == $row["id"]) {
				?><td><textarea name="content" rows="30" style='width:100%'><? echo $row["content"]; ?> </textarea>
				<input type="submit" name="qr_update_c" value="Update" ></input></td> <?
			} else {
				
				?><td><div class="yui-test"><? echo stripslashes($row["content"]); ?> </div></td><?
				
			}
			
		} else {
			if ($_GET["rowid"] == $row["id"]) {
				?> <td><input name="content" type="text" value=" <? echo $row["content"]; ?>" style='width:100%'></input>
				<br><input type="submit" name="qr_update_c" value="Update" ></input></td> <?
			} else {
				echo "<td>".$row["content"]."</td>";
			}
				
		}
		
		echo "</p></tr>";
	}
	echo "</table>";
	mysql_free_result($result);	
	
	echo '<br>';
	echo '<table width=100%>';
	echo "<tr><td colspan='3' bgcolor=\"#8ED9F9\">Acceptance Section</td></tr>";
	echo "<tr><td>data</td><td>semnatura</td></tr>";
	echo '</form></table>';
	
	
	?>

    </div>
	
<div class="yui-u">
 <!-- AL DOILEA ECRAN!!! -->

	<?
	//####afisam sectiunea add/update/delete:
	if(isset($_POST['add'])) 
	{
		unset($_GET[rowid]);
		//echo $idd;
		display($idd);
	//display($_GET['']);
	}



dbClose($dbh);
	?>
 <br>
     </div>
</div>
</div>
 
	

 </div>
  
</div>
</body>
</html>
<? include 'footer.php';
?>